package cn.edu.nju.sharing;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;

public class StartUpProvider {
		private static StartUpProvider startUpProvider;
		private Activity activity;
		private String serverUrl;
		
		private StartUpProvider(Activity activity){
			this.activity = activity;
			serverUrl = activity.getString(R.string.server_url) + "login.php";
		}
		
		public static StartUpProvider getInstance(Activity activity){
			if(startUpProvider == null){
				startUpProvider = new StartUpProvider(activity);
			}
			return startUpProvider;
		}
		
		//Used when register a new user, if don't have an avatar ,set it null
		public boolean register(String name, String passwrod,File avatar){
			try {
				HttpClient client = new DefaultHttpClient();
				HttpContext localContext = new BasicHttpContext();
				HttpPost post = new HttpPost(serverUrl);

				MultipartEntity entity = new MultipartEntity(
						HttpMultipartMode.BROWSER_COMPATIBLE);

				entity.addPart("option", new StringBody("register"));
				entity.addPart("name", new StringBody(name));
				entity.addPart("password",new StringBody(passwrod));
				
				if(avatar == null){
					entity.addPart("hasAvatar",new StringBody("false"));
				} else {
					entity.addPart("hasAvatar",new StringBody("true"));
					entity.addPart("avatar",new FileBody(avatar));
				}
				
				post.setEntity(entity);
				HttpResponse response = client.execute(post, localContext);
				HttpEntity httpEntity = response.getEntity();
				InputStream input = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						input));

				String context = reader.readLine();
				reader.close();
				
//				Toast.makeText(activity, name, 100001).show();
				
				if(context.equals(new String("OK"))){
					return true;
				} else if(context.equals(new String("fail"))){
					return false;
					
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		
		//Return the name and password of the most recent user log in, if no such user, return null
		public Map<String, String> getRecentLoginUser(){
			SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
			String name = sharedPreferences.getString("recent_user_name", "none");
			String password = sharedPreferences.getString("recent_user_password","none");
			
			if(!name.equals(new String("none")) && !password.equals(new String("none"))){
				Map<String, String>map = new HashMap<String, String>();
				map.put("name", name);
				map.put("password", password);
				return map;
			}
			
			return null;
		}
		
		//Return the id of the user if log in successfully,otherwise return -1
		public long login(String name,String password){
			try {
				HttpClient client = new DefaultHttpClient();
				HttpContext localContext = new BasicHttpContext();
				HttpPost post = new HttpPost(serverUrl);

				MultipartEntity entity = new MultipartEntity(
						HttpMultipartMode.BROWSER_COMPATIBLE);

				entity.addPart("option", new StringBody("login"));
				entity.addPart("name",new StringBody(name));
				entity.addPart("password", new StringBody(password));
				
				post.setEntity(entity);
				HttpResponse response = client.execute(post, localContext);
				HttpEntity httpEntity = response.getEntity();
				InputStream input = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						input));

				String context = reader.readLine();
				reader.close();
				
				if(!context.equals(new String("fail"))){
					SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
					Editor editor = sharedPreferences.edit();
					editor.putString("recent_user_name", name);
					editor.putString("recent_user_password", password);
					editor.putLong("id", Long.parseLong(context));
					editor.commit();
					return Long.parseLong(context);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return -1;
		}
		
		public boolean clearCurrentUser(){
			SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.putString("recent_user_name", "none");
			editor.putString("recent_user_password", "none");
			editor.commit();
			
			return true;
		}
}
